package com.egame.manage.biz.server.gc;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.model.PageData;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.EnumType.GameStatus;
import cn.egame.interfaces.cp.ContentProviderInfo;
import cn.egame.interfaces.gc.GameInfo;
import cn.egame.interfaces.pu.AppParameter;
import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.domain.ImageInfo;

public class GameServiceImpl {
	
	private static GameServiceImpl instance = null;
	private static byte[] SyncRoot = new byte[1];
	private GameServiceDao dao = null;
	
	public static GameServiceImpl getInstance() throws ExceptionCommonBase {
		if (instance == null) {
			synchronized (SyncRoot) {
				if (instance == null) {
					instance = new GameServiceImpl();
				}
			}
		}
		return instance;
	}
	
	public GameServiceImpl() throws ExceptionCommonBase{
		super();
		dao = new GameServiceDao();
	}
	
	public PageData pageGameList(int appId,long loginUserId,GameInfo bean,String shortName, int page,int rowsOfPage)throws RemoteException{
		List<Integer> ids = dao.listGameList(appId, loginUserId, bean, shortName ,page, rowsOfPage);
		List<GameInfo> gameList = this.listGameInfoByIds(appId, loginUserId, ids);		
		int total = dao.getGameCount(bean,shortName);
		PageData pd = new PageData(page, total, rowsOfPage);
		pd.setContent(gameList);
		return pd;
	}
	
	public int updateGameLabel(int appId,long loginUserId,int gId, String label)throws ExceptionCommonBase{ 
		return dao.updateGameLabel(gId, label);
	}

	
	public int updateGameStatus(int appId,long loginUserId,int gId,int gameStatus)throws ExceptionCommonBase{
		String[] gIds = new String[1];
		gIds[0] = "" + gId;
		return dao.updateGameStatus(gIds, gameStatus);
	}
	
	public int updateGameStatus(int appId,long loginUserId,String[] gIds,int gameStatus)throws ExceptionCommonBase{
		return dao.updateGameStatus(gIds, gameStatus);
	}
	
	public int setGameImage(int appId,long loginUserId,long efsId,int gameId)throws ExceptionCommonBase{
		return dao.setGameImage(loginUserId, efsId, gameId);
	}
	
	public Map<String, Object> listCP()throws ExceptionCommonBase{
		return dao.listCP();				
	}
	
	public Map<String, Object> listCpIdAndName(String shortName)throws ExceptionCommonBase{
		return dao.listCpIdAndName(shortName);				
	}
	
	public ContentProviderInfo getContentProviderInfoById(int appId,long loginUserId,int cpId)throws RemoteException{
		ContentProviderInfo bean = ManageClient.getInstance().getContentProviderInfoById(appId, loginUserId, cpId);
		if(bean == null) bean = new ContentProviderInfo();
		return bean;
	}
	
	public String generateGameCode()throws ExceptionCommonBase{
		return dao.generateGameCode();
	}
	
	public List<GameInfo> listGameList(int appId,long loginUserId,GameInfo bean)throws RemoteException{
		List<Integer> ids = dao.listGameList(bean);
		return this.listGameInfoByIds(appId, loginUserId, ids);
	}
	
	public List<Integer> listGameIdList(GameInfo bean)throws ExceptionCommonBase{	
		return dao.listGameList(bean);
	}
	
	public int getGameCount(List<Integer> statusList) throws ExceptionCommonBase{
		return dao.getGameCount(statusList);
	}
	
	public int getGameCount(GameStatus status) throws ExceptionCommonBase{
		if(status != null){
			List<Integer> statusList = new ArrayList<Integer>();
			statusList.add(status.value());
			return dao.getGameCount(statusList);
		}
		return -1;
	}
	
	//批量更新游戏标签
	public void updateGameLabels(int appId, long loginUserId, Map<Integer,String> map)throws ExceptionCommonBase{		
		Set<Integer> keys = map.keySet();
		for(Integer key:keys){
			dao.updateGameLabel(key, map.get(key));
		}       
	}
	
	//导出游戏标签报表接口
	public List<String[]> getGameNameAndLabel(int appId,long loginUserId) throws ExceptionCommonBase{
		return dao.getGameNameAndLabel();
	}
	
	public int updateGameFirstOnLineTime(int gId,long time)throws ExceptionCommonBase{
		return dao.updateGameTime("first_online_time", gId, time);
	}
	
	public int updateGameLastOnLineTime(int gId,Date date)throws ExceptionCommonBase{
		return dao.updateGameTime("last_online_time", gId, date);
	}
	
	public int updateGameLastOffLineTime(int gId,Date date)throws ExceptionCommonBase{
		return dao.updateGameTime("last_offline_time", gId, date);
	}
	
	public int updateGamePublishTime(int gId,Date date)throws ExceptionCommonBase{
		return dao.updateGameTime("publish_time", gId, date);
	}
	
	public String[] getFilePath(int appId,Long loginUserId,long efsId)throws Exception{
		return dao.getFileNameAndPath(appId, loginUserId, efsId);
	}
	
	public String getImagePath(int appId,Long loginUserId,long efsId)throws Exception{
		return dao.getFilePath(appId, loginUserId, efsId);
	}

	public ImageInfo getSmallImageInfoById(int appId,Long loginUserId,long efsId) throws Exception{
		return dao.getSmallImageInfoById(appId, loginUserId, efsId);
	}
	
	public AppParameter getAppParameterByGId(int appId,Long loginUserId,int gId)throws RemoteException{
		return ManageClient.getInstance().getAppParameterByGId(appId, loginUserId, gId);
	}
	
	public List<GameInfo> listGameInfoByIds(int appId,Long loginUserId,List<Integer> gids)throws ExceptionCommonBase{
		return dao.listGameInfoByIds(gids);
	}
	
	public GameInfo getGameInfoById(int appId,Long loginUserId,long gid) throws ExceptionCommonBase {
		return dao.getGameInfoById(gid);
	}
	
	public ImageInfo getImageInfoById(int appId,Long loginUserId,long efsId) throws Exception{
		return dao.getImageInfoById(appId, loginUserId, efsId);
	}
	
	public List<Integer> listGameIdsByGameStatus(List<Integer> ids, GameStatus status)throws ExceptionCommonBase{
		return dao.listGameIdsByGameStatus(ids, status);
	}

	public int getGameCount() throws ExceptionCommonBase{
		return dao.getGameCount(null);
	}
	
	public String getLocalFilePath(int appId,Long loginUserId,long efsId)throws Exception{
		return dao.getLocalFilePath(appId, loginUserId, efsId);
	}
	
	public int updateTCLAppId(int gid,String tclAppId)throws ExceptionCommonBase{
		return dao.updateTCLAppId(gid, tclAppId);
	}
	
	public String getTCLAppIdByPackageName(String entityPackageName) {
		return dao.getTCLAppIdByPackageName(entityPackageName);
	}
	
	public List<GameInfo> listGameInfo(int appId,Long loginUserId,GameInfo bean)throws ExceptionCommonBase{
		List<Integer> ids = dao.listGameIds(bean);
		if(ids != null && ids.size()>0){
			return GameServiceImpl.getInstance().listGameInfoByIds(appId, loginUserId, ids);
		}
		return new ArrayList<GameInfo>();
	}
	
	public int updatePriceByGid(int appId,long loginUserId,int gid,int price)throws ExceptionCommonBase{
		return dao.updatePriceByGid(gid, price);
	}
		
	public List<Integer> listGidByRandom(int appId,long loginUserId)throws ExceptionCommonBase{
		return dao.listGidByRandom();
	}

	public List<Integer> listGIdByName(int appId,long loginUserId,int gid,String name,int status)throws ExceptionCommonBase{	
		return dao.listGIdByName(gid, name, status);
	}
	
	public List<Integer> listGIdByTypeAndName(int appId,long loginUserId,int gid,String name,int status,int gameType) throws ExceptionCommonBase{
		return dao.listGIdByTypeAndName(gid, name, status, gameType);
	} 
	
	public String getCpCodeByGid(int gid)throws ExceptionCommonBase{
		return dao.getCpCodeByGid(gid);
	}
}